﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Util
{
    public class CookieHelper
    {
        #region 读取或写入cookie
        /// <summary>
        /// 清除指定Cookie
        /// </summary>
        /// <param name="cookiename">cookiename</param>
        public static void ClearCookie(string strName)
        {
            HttpCookie cookie = new HttpCookie(strName);
            var domain = HttpContext.Current.Request.Url.Host.ToString();
            if (cookie != null)
            {
                cookie.Domain = domain;
                cookie.Path = "/";
                cookie.Value = "";
                cookie.Expires = DateTime.Now.AddYears(1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }

            cookie = HttpContext.Current.Request.Cookies[strName];
            if (cookie != null && !string.IsNullOrEmpty(cookie.Value))
            {
                cookie.Domain = domain;
                cookie.Path = "/";
                cookie.Value = "";
                cookie.Expires = DateTime.Now.AddYears(1);
                HttpContext.Current.Response.Cookies.Add(cookie);
            }
        }
        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        public static void WriteCookie(string strName, string strValue)
        {
            HttpCookie cookie = new HttpCookie(strName);
            var domain = HttpContext.Current.Request.Url.Host.ToString();
            cookie.Domain = domain;
            cookie.Value = Common.UrlEncode(strValue);
            cookie.Path = "/";
            HttpContext.Current.Response.AppendCookie(cookie);
        }


        /// <summary>
        /// 写cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <param name="strValue">值</param>
        /// <param name="strValue">过期时间(分钟)</param>
        public static void WriteCookie(string strName, string strValue, int expires)
        {
            HttpCookie cookie = new HttpCookie(strName);
            var domain = HttpContext.Current.Request.Url.Host.ToString();
            cookie.Domain = domain;
            cookie.Path = "/";
            cookie.Value = Common.UrlEncode(strValue);
            cookie.Expires = DateTime.Now.AddMinutes(expires);
            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 读cookie值
        /// </summary>
        /// <param name="strName">名称</param>
        /// <returns>cookie值</returns>
        public static string GetCookie(string strName)
        {
            if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
            {
                return Common.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString());
            }
            return "";
        }

        #endregion
    }
}
